//
//  Pet_PublishCardViewController.swift
//  PetApp
//
//  Created by mac on 2023/9/18.
//  Copyright © 2023 Pet_. All rights reserved.
//

import UIKit

class Pet_PublishCardViewController: BasicViewController {

    var imgArr:[String] = []
    var typeString = ""
    var descString = ""

    private lazy var table: UITableView = {
        let view = UITableView(frame: .zero, style: .plain)
        view.separatorStyle = .none
        view.backgroundColor = .clear
        view.delegate = self
        view.dataSource = self
        view.showsVerticalScrollIndicator = false
        view.register(PublishTopCell.self, forCellReuseIdentifier: PublishTopCell.description())
        view.register(PublishBottomCell.self, forCellReuseIdentifier: PublishBottomCell.description())
        view.register(PublishMiddleCell.self, forCellReuseIdentifier: PublishMiddleCell.description())
        return view
    }()
    private lazy var doneButton: UIButton = {
        let button = UIButton(type: .custom)
        button.titleLabel?.font = .setTypeFace(16.Fit,type: .medium)
        button.setTitleColor(mainColor, for: .normal)
        button.setTitle("Post", for: .normal)
        button.backgroundColor = .black
        button.layer.cornerRadius = 25
        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.subviews {
            table
            doneButton
        }
        doneButton.left(30.Fit).right(30.Fit).height(50).bottom(TabbarSafeBottomMargin + 5.Fit)
        doneButton.addAct {
            Toast_View_Tool.show()
            
            guard self.typeString.count != 0 else {
                Toast_View_Tool.show(type: .error,text: "The type cannot be empty.")
                return
            }
            guard self.descString.count != 0 else {
                Toast_View_Tool.show(type: .error,text: "The description cannot be empty.")
                return
            }
            guard self.imgArr.count != 0 else {
                Toast_View_Tool.show(type: .error,text: "The picture cannot be empty.")
                return
            }
            Toast_View_Tool.show()
            let date = Date()
            let timeFormatter = DateFormatter()
            timeFormatter.dateFormat = "MM-dd HH:mm"
            let strNowTime = timeFormatter.string(from: date) as String
            var dict:[String:Any] = [:]
            dict["userId"] = USEINFOMODEL.id
            dict["icon"] = USEINFOMODEL.headImage
            dict["info"] = USEINFOMODEL.name
            dict["info0"] = self.typeString
            dict["info1"] = self.descString
            dict["info2"] = self.imgArr.joined(separator: "@@")
            dict["info3"] = "0"
            dict["info4"] = strNowTime
            dict["password"] = "0"
            RT.createHouse(paraInfo: dict) { s in
                Toast_View_Tool.show(type: .success,text: "Success")
                self.navigationController?.popViewController(animated: true)
            }
        }
        table.left(0).right(0).top(0)
        table.Bottom == doneButton.Top - 10.Fit
        table.reloadData()
    }
    
}

extension Pet_PublishCardViewController:UITableViewDelegate,UITableViewDataSource{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 1{
            let cell = tableView.dequeueReusableCell(withIdentifier: PublishMiddleCell.description(), for: indexPath) as! PublishMiddleCell
            cell.imgArr = self.imgArr
            cell.upImgBlock = { img in
                self.imgArr.append(img)
                self.table.reloadData()
            }
            cell.deleteBlock = { index in
                self.imgArr.remove(at: index)
                self.table.reloadData()
            }
            return cell
        }
        if indexPath.section == 2{
            let cell = tableView.dequeueReusableCell(withIdentifier: PublishBottomCell.description(), for: indexPath) as! PublishBottomCell
            cell.doneBlock = { name in
                self.descString = name
            }
            return cell
        }
        let cell = tableView.dequeueReusableCell(withIdentifier: PublishTopCell.description(), for: indexPath) as! PublishTopCell
        cell.doneBlock = { name in
            self.typeString = name
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 2{
            return 160.Fit
        }
        if indexPath.section == 1{
            var h:CGFloat = 0
            if imgArr.count/2 == 0{
                h = CGFloat((imgArr.count/2 + 1)) * 170.Fit + 65.Fit
            }else{
                h = CGFloat(imgArr.count/2 + 1) * 170.Fit + 65.Fit
            }
            print("heightForRowAt==-======   ")
            print(imgArr.count/2)
            print(h)
            return h
        }
        return 100.Fit
    }
}
